<template>
    <div class="allotPower public">
        <!-- 面包屑 -->
      	<el-breadcrumb separator=">" class="crumbs">
            <el-breadcrumb-item :to="{ path: '/role'}">角色管理</el-breadcrumb-item>
      	  	<el-breadcrumb-item :to="{ path: '/allotPower'}">分配权限</el-breadcrumb-item>
      	</el-breadcrumb>
        <el-row class="powerList">
            <el-col v-for="(item,index) in permissionGroup" :span="24">
                <el-col :span="24"  class="powerSys">
                    {{item.name}}
                </el-col>
                <el-col :offset="1" :span="22" v-for="(mod,index) in item.modules">
                    <el-col :span="24"  class="powerMod">
                        {{index+1}}.{{mod.name}}模块
                    </el-col>
                    <!-- <el-col :span="24"  class="powerMod">
                        <el-checkbox v-model="mod.permissions.checkAll" @change="checkedAll(mod,1)">全选</el-checkbox>
                    </el-col> -->


                    <el-col :offset="1" :span="7" v-for="permission in mod.permissions">
                         <el-checkbox v-model="permission.checked" @change="checkedAll(mod,0)">{{permission.permissionName}}</el-checkbox>
                    </el-col>
                </el-col>
            </el-col>

        </el-row>
        <el-col :offset="11" :span="2"><el-button @click="submit()" class="sunmit">提交</el-button></el-col>

    </div>
</template>


<script>
    export default {
        name:'allotPower',
        data () {
            return{
                permissionGroup:[],
                checked:[]
            }
        },
        methods:{
            checkedAll (o,num) {
                if(num){

                }
            },
            submit () {
                for(let i = 0;i < this.permissionGroup.length; i++){
                    for(let j = 0;j < this.permissionGroup[i].modules.length; j++){
                        for(let k = 0;k < this.permissionGroup[i].modules[j].permissions.length; k++){
                            if(this.permissionGroup[i].modules[j].permissions[k].checked){
                                this.checked.push(this.permissionGroup[i].modules[j].permissions[k].id)
                            }
                        }
                    }
                }
                var params = {
                    id : this.$route.query.id,
                    permissionList : this.checked
                }
                this.$http.post(this._path + '/role/updatePermissionToRole',params).then((res)=> {
                    if('0000' == res.data.resultCode){
                        this.$message({
							message:'保存成功',
							type:'success',
							duration:1500,
							showClose:true
						})
                        this.$router.push('/role')
                    }else{
                        this.requestError(res,this)
                    }
                })
            }
        },
        created () {
            var params = {
                roleId:this.$route.query.id
            }
            // 进入页面，获取所有权限,并操作所有选中的按钮

            this.$http.post(this._path + '/role/selectAllPermissions',params).then((res) => {
                if('0000' == res.data.resultCode){
                    this.permissionGroup = res.data.data.permissionGroup;
                    for(let i = 0;i < this.permissionGroup.length; i++){
                        for(let j = 0;j < this.permissionGroup[i].modules.length; j++){
                            if (this.permissionGroup[i].modules[j].checked == '0') {
                                this.permissionGroup[i].modules[j].checked = false;
                            }
                            if (this.permissionGroup[i].modules[j].checked == '1') {
                                this.permissionGroup[i].modules[j].checked = true;
                            }
                            for(let k = 0;k < this.permissionGroup[i].modules[j].permissions.length; k++){
                                if('1' === this.permissionGroup[i].modules[j].permissions[k].checked){
                                    this.permissionGroup[i].modules[j].permissions[k].checked = true
                                }else if('0' === this.permissionGroup[i].modules[j].permissions[k].checked){
                                    this.permissionGroup[i].modules[j].permissions[k].checked = false
                                }
                            }
                        }
                    }
                }else{
                    this.requestError(res,this)
                }

            })
        },
        mounted(){
            $(function() {
                // $("body").on("click", ".p-all", function() {
                //     var _checked = $(this).attr("checked");
                //     if 选中
                //       子div的所有checkbox 选中
                //     else
                //      子div的所有checkbox 取消选中
                // }).on("click", ".p-children", function() {
                //     if 当前节点 所有兄弟系欸但选中
                //       $(".p-all") 选中
                //     否则
                //       $(".p-all") 取消选中
                // });

                // $(".p-all").attr("checked", $(".p-children:not:checked").length ==0 ? true : false)
            });
        }
    }
</script>
<style scoped lang="scss">
// 没有这个文件:
@import "./../style/powerList.scss";
@import "./../style/public.scss"


</style>
